In [1]:
N = 1000000
prime = [False, False] + [True] * (N - 2)
primes = []
for p in range(2, N):
if prime[p]:
primes.append(p)
for q in range(p*p, N, p):
prime[q] = False
count = 0
pow10 = 1
k_max = 1
for p in primes:
k = len(str(p))
if k > k_max:
pow10 *= 10
k_max = k
for i in range(k-1):
p = p//10 + (p%10) * pow10
if not prime[p]:
break
else:
count += 1
print(count)
In [ ]: